//
//  ShareEngine.m
//  YouHui
//
//  Created by  on 11-12-22.
//  Copyright (c) 2011年 netease. All rights reserved.
//

#import "ShareEngine.h"
#import "ShareError.h"
#import "OAConsumer.h"
#import "OAToken.h"
#import "OAuthEngine.h"
#import "ShareClient.h"
#import "ShareAccount.h"

@interface ShareEngine()
@property(nonatomic,readonly) OAuthEngine* oAuthEngine;
@property(nonatomic,readonly) ShareClient* client;
@property(nonatomic,retain) OAToken *accessToken;
@end

@implementation ShareEngine

@synthesize  baseViewController=_baseViewController;
@synthesize shareType=_shareType;
@synthesize  delegate=_delegate;
@synthesize  accessToken=_accessToken;
@synthesize account=_account;
@dynamic  hasBind;
@dynamic oAuthEngine;

- (id)initWithType:(NSString*)shareType {
	if (self = [super init]) {
        _shareType = [shareType retain];
        _account = [[ShareAccount accountWithType:_shareType] retain];
        
        self.accessToken = [[OAToken alloc] initWithHTTPResponseBody:_account.authorizeData];
        if (![_accessToken isValid]) {
            self.accessToken = nil;
        }
        
        _OAConsumer = [[OAConsumer alloc] initWithKey:_account.consumerKey secret:_account.consumerSecret];
	}
	return self;
}

-(void)dealloc {
    [_OAuthEngine release];
    [_OAConsumer release];
    [_shareType release];
    [_client release];
    [super dealloc];
}

-(void) bindAccount{
    [self.oAuthEngine startAuthorize];
}

- (BOOL)hasBind {
    if (_accessToken&&[_accessToken isValid]) {
        return YES;
    }
    return NO;
}

-(void)setBaseViewController:(UIViewController *)baseViewController {
    _baseViewController = baseViewController;
    self.oAuthEngine.baseViewController = _baseViewController;
}

-(OAuthEngine*)oAuthEngine {
    if (!_OAuthEngine) {
        _OAuthEngine = [[OAuthEngine OAuthEngineForType:_shareType delegate:self] retain];
        _OAuthEngine.consumer = _OAConsumer;
        _OAuthEngine.delegate = self;
    }
    return _OAuthEngine;
}

-(ShareClient*)client {
    if (!_client) {
        if (_OAConsumer&&_accessToken) {
            _client = [[ShareClient clientForType:_shareType 
                                         withConsumer:_OAConsumer 
                                         andToken:_accessToken] retain];
            _client.delegate = self;
        }
    }
    return _client;
}

-(void) upload:(NSString *)text image:(UIImage *)image{
    if (image) {
        [self.client upload:UIImageJPEGRepresentation(image,0) status:text];
    } else {
        [self.client post:text];   
    }
}


-(void)shareClientFinished:(ShareClient*)sender{
    if (sender.error==nil) {
        _account.screenName = sender.screenName;
    }
    if (_delegate) {
        [_delegate shareEngine:self uploadFinished:sender.error];
    }
}

//=============================================================================================================================
#pragma mark OAuthEngineDelegate
- (void)OAuthEngine:(OAuthEngine*)engine authrizedWithData: (NSString*)data {
    self.accessToken = [[OAToken alloc] initWithHTTPResponseBody:data];
    _account.authorizeData = data;
    [_delegate shareEngine:self bindFinished:nil];
}

- (void)OAuthEngine:(OAuthEngine *)engine willPresentOAuthController:(OAuthController*)controller {
    
}

- (void)OAuthEngine:(OAuthEngine *)engine failedWithError:(NSError*)error {
    NSLog(@"%@",[error localizedDescription]);
}

- (void)OAuthEngineCancel:(OAuthEngine *)engine {
    
}

@end
